package com.tuanzi.loan.web.bean.family;

import com.base.common.http.HttpClientUtil;
import com.base.common.http.model.RequestHeader;
import com.base.common.log.LogService;
import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.family.LoanLargeH5StatService;
import com.tuanzi.loan.vo.request.family.LoanLargeH5StatSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanLargeAmountPartnerControlSearchResponse;
import com.tuanzi.loan.vo.response.family.LoanLargeH5StatSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.LoanLargeH5StatLazyDataModel;
import com.tuanzi.server.loan_base.util.DateUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanLargeH5StatCommentBean extends JSFListingBean<LoanLargeH5StatSearchRequest, LoanLargeH5StatSearchResponse> {
    @Value("${loan.large.amount.waitui.url}")
    private String url;
    @Autowired
    LoanLargeH5StatService loanLargeH5StatService;



    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onFresh() {
        try {
            HttpClientUtil.get(new RequestHeader(),String.format(url,DateUtils.getCurrentDate()));
            addInfoMessage("刷新成功！");
        } catch (Exception e) {
            LogService.error("刷新失败",e);
        }
    }

    private void initLazyDataModel() {
        $ = new LoanLargeH5StatSearchRequest();
        try {
            $.setStartDate(DateUtils.formatYyyyMMddToDate(DateUtils.getDateBeforeDays(1)));
            $.setEndDate(DateUtils.formatYyyyMMddToDate(DateUtils.getCurrentDate()));
        } catch (ParseException e) {
            LogService.error("日期转换异常",e);
        }
        lazyDataModel = new LoanLargeH5StatLazyDataModel($);
    }
    public List<String> queryProName(){
        return loanLargeH5StatService.queryProName();
    }

    public List<String> selectChannelInfo(){
        return loanLargeH5StatService.selectChannelInfo();
    }

    public boolean isShow(String cfrom){
        LoanLargeAmountPartnerControlSearchResponse resp
                = loanLargeH5StatService.findCurrentNameInLargeAmount();
        if(resp == null){
            return true;
        }
        if(StringUtils.isBlank(cfrom)){
            return false;
        }
        if ("iscommit".equals(cfrom) && "是".equals(resp.getIscommit())){
            return true;
        }
        if ("isapprove".equals(cfrom) && "是".equals(resp.getIsapprove())){
            return true;
        }
        if ("issuccess".equals(cfrom) && "是".equals(resp.getIssuccess())){
            return true;
        }

        if ("isdistinct".equals(cfrom) && "是".equals(resp.getIsDistinct())){
            return true;
        }
        return false;
    }

    public boolean isMeumShow(){
        if (loanLargeH5StatService.findCurrentNameInLargeAmount() == null){
            return true;
        }
        return false;
    }

    public List<String> findChannalnd(){
        List<String> list = Lists.newArrayList();
        String channelnd = null;
        try {
            channelnd = loanLargeH5StatService.findCurrentNameInLargeAmount().getChannel_n();
            if (StringUtils.isNotBlank(channelnd)){
                Arrays.stream(channelnd.split(",")).forEach(arr->{
                    list.add(arr.trim());
                });
            }
        } catch (Exception e) {
            addErrorMessage("当前用户没有配置代理渠道");
        }
        return list;
    }
}
